package com.aaa.service.impl;

import com.aaa.dao.OperLogDao;
import com.aaa.entity.OperLog;

import com.aaa.service.OperLogService;
import com.aaa.vo.OperLogVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class OperLogServiceImpl implements OperLogService {
    @Autowired
    private OperLogDao operLogDao;
    @Override
    public Boolean insert(OperLog operLog) {
        return operLogDao.insert(operLog)>0;
    }

    @Override
    public Result<IPage<OperLog>> queryOperLogByPage(OperLogVo logVo) {
        Page<OperLog> operLogPage = new Page<>(logVo.getPageNum(),logVo.getPageSize());
        QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(logVo.getTitle())){
            wrapper.like("title",logVo.getTitle());
        }
        if (StringUtils.hasText(logVo.getTitle())){
            wrapper.eq("status",logVo.getStatus());
        }if (StringUtils.hasText(logVo.getOperName())){
            wrapper.like("oper_name",logVo.getOperName());
        }

        if (StringUtils.hasText(logVo.getTitle())){
            wrapper.eq("business_type",logVo.getBusinessType());
        }
        if (logVo.getBeginTime()!=null&&logVo.getEndTime()!=null){
            wrapper.between("oper_time",logVo.getBeginTime(),logVo.getEndTime());
        }
        Page<OperLog> operLogPage1 = operLogDao.selectPage(operLogPage, wrapper);




        return new Result<>(2000,"ok",operLogPage1);
    }

    @Override
    public Result<String> deleteOperLogByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = operLogDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result<String> delAll() {
        QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
        int delete = operLogDao.delete(wrapper);
        if (delete>0){
            return Result.success();
        }
        return Result.error();
    }
}
